package com.diipo.weibo.adapter;

import java.util.ArrayList;

import com.diipo.weibo.AsyncImageLoader;
import com.diipo.weibo.ConfigInfo;
import com.diipo.weibo.R;
import com.diipo.weibo.entity.Comments;
import com.diipo.weibo.entity.Details;
import com.diipo.weibo.utils.TimeUtil;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailsItemAdapter extends BaseAdapter{
	
	private Context mctx;
	private Comments[] mArrayComments;
	private AsyncImageLoader asyncImageLoader = AsyncImageLoader.getInstance();
	
	public DetailsItemAdapter(Comments[] comments,Context context) {
		super();
		this.mArrayComments = comments;
		this.mctx = context;
	}


	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArrayComments == null ? 0 : mArrayComments.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mArrayComments[position];
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		DetailsItem item = null;
		if (convertView == null) {
			item = new DetailsItem();
			LayoutInflater inflater = LayoutInflater.from(mctx);
			convertView = View.inflate(mctx, R.layout.weibo_details_item, null);
			item.icon = (ImageView) convertView.findViewById(R.id.details_item_icon);
			item.name = (TextView) convertView.findViewById(R.id.details_item_name);
			item.content = (TextView) convertView.findViewById(R.id.details_item_content);
			item.comment_content_type = (TextView)convertView.findViewById(R.id.detail_comment_content_type);
			item.time = (TextView)convertView.findViewById(R.id.time);
		}
		convertView.setTag(position);
		final Comments comment = mArrayComments[position];
		String content_type = comment.getContent().getType();
		if(content_type.equals(ConfigInfo.COMM.CONTENT_TYPE_LINK)){ //link 0
			item.comment_content_type.setVisibility(View.VISIBLE);
			if(comment.getContent().getUploadfile().equals("1")){
				item.comment_content_type.setText(mctx.getString(R.string.uploadfile ));
			}else{
				item.comment_content_type.setText(mctx.getString(R.string.link ));
			}
		}else if(content_type.equals(ConfigInfo.COMM.CONTENT_TYPE_PIC)){ //picture 2
			item.comment_content_type.setVisibility(View.VISIBLE);
			item.comment_content_type.setText(mctx.getString(R.string.picture ));
		}else if(content_type.equals(ConfigInfo.COMM.CONTENT_TYPE_VEDIO)){ //vedio 3
			item.comment_content_type.setVisibility(View.VISIBLE);
			item.comment_content_type.setText(mctx.getResources().getString(R.string.vedio ));
		}else if(content_type.equals(ConfigInfo.COMM.CONTENT_TYPE_RICH_HTML) ||
				content_type.equals(ConfigInfo.COMM.CONTENT_TYPE_RICH_EMBEDDED) ||
				content_type.equals(ConfigInfo.COMM.CONTENT_TYPE_RICH_FORM)){ //rich HTML
			item.comment_content_type.setVisibility(View.VISIBLE);
			item.comment_content_type.setText(mctx.getResources().getString(R.string.rich_text));
		}else{
			item.comment_content_type.setVisibility(View.GONE);
		}
		asyncImageLoader.loadPortrait(comment.getUser().getProfile_picture(), item.icon);
		item.name.setText(comment.getUser().getUsername());
		item.time.setText("2010-12-23");
		if(comment.getContent().getText().length() >=140){
//			item.content.setText(comment.getContent().getText().substring(0,139) + mctx.getResources().getString(R.string.check_more));
			String tmp = comment.getContent().getText().substring(0,139) + mctx.getResources().getString(R.string.check_more);
	        SpannableString ss = new SpannableString(tmp);
	        ss.setSpan(new ForegroundColorSpan(0xff4149aa),140, tmp.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
	        item.content.setText(ss);
		}else{
			item.content.setText(comment.getContent().getText());
		}
		return convertView;
	}

}
